package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.Report;
import com.api.shopapi.entity.ReportImg;
import com.api.shopapi.entity.dto.ReportDTO;
import com.api.shopapi.mapper.ReportImgMapper;
import com.api.shopapi.mapper.ReportMapper;
import com.api.shopapi.service.ReportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("ReportServiceImplOne")
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ReportImgMapper reportImgMapper;

    @Override
    @Transactional
    public int insert(Report report, JSONArray reportImgList) {
        for (int i=0;i<reportImgList.size();i++){
            String url = reportImgList.get(i).toString();
            ReportImg reportImg=new ReportImg();
            reportImg.setReportImgCode(UUID.randomUUID().toString());
            reportImg.setReportCode(report.getReportCode());
            reportImg.setUrl(url);
            reportImg.setSort(i+1);
            reportImgMapper.insert(reportImg);
        }
        return reportMapper.insert(report);
    }

    @Override
    public int update(Report report) {
        return reportMapper.update(report);
    }

    @Override
    public int delete(String reportCode) {
        return reportMapper.delete(reportCode);
    }

    @Override
    public Report getByReportCode(String reportCode) {
        return reportMapper.getByReportCode(reportCode);
    }

    @Override
    public PageInfo<Report> getPageList(int pageIndex, int pageSize, String userCode) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Report> reportList = reportMapper.getPageList(userCode);
        return new PageInfo<>(reportList);
    }

    @Override
    public PageInfo<ReportDTO> GetPageByGovernment(int pageIndex, int pageSize, String userName, String governmentCode) {
        PageHelper.startPage(pageIndex,pageSize);
        List<ReportDTO> reportDTOList = reportMapper.GetPageByGovernment(userName,governmentCode);
        return new PageInfo<>(reportDTOList);
    }
}
